/*
 * @(#)DefaultMessageSender.java   1.0  2022年1月12日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.core.service.processor.send;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.swift.core.model.HttpServiceRequest;
import com.swift.core.model.ServiceRequest;
import com.swift.core.model.ServiceResponse;
import com.swift.exception.extend.SystemException;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2022年1月12日
 */
@Component
@Primary
public class DefaultMessageSender implements MessageSender {

    @Autowired(required=false)
    @Qualifier("applicationJsonHandler")
    private MessageSender applicationJsonHandler;
    
    @Autowired(required=false)
    @Qualifier("keepaliveMessageDeliverImpl")
    private KeepaliveMessageSender keepaliveMessageSender;
    
    /** 
     * @see com.swift.core.service.processor.send.MessageSender#sendResponse(com.swift.core.model.ServiceResponse)
     */
    @Override
    public void sendResponse(ServiceResponse response) {
       ServiceRequest req = response.getRequest();
       if(req==null) throw new SystemException("返回的响应没设置对应请求");
       if(req instanceof HttpServiceRequest) {
           if(applicationJsonHandler==null) throw new SystemException("HTTP服务不存在");
           applicationJsonHandler.sendResponse(response);
           return;
       }
       if(keepaliveMessageSender==null) throw new SystemException("未建立长连接服务");
       keepaliveMessageSender.sendResponse(response);
    }

}
